<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentProfileDesign.class.php, $Author:  $ */



/** User profile design component
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentProfileDesign.class.php 406 2008-03-17 20:15:00Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentProfileDesign extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
        if (App::getSetting('allow_css_edit'))
        {
            App::gotoUrl('public.account');
        }

        $checkUsers = $this->oDatabase->getRow("SELECT user FROM ". App::getT('template') ." WHERE user = '". App::getUser('user') ."'");

        if ( empty($checkUsers['user']) )
		{
        	$this->oDatabase->query("INSERT INTO ". App::getT('template') ." SET user = '". App::getUser('user') ."'");
		}

	    $oAcc = $this->getParam('oUser');
	    /* @var $oAcc PhpFox_Mod_Account_ItemAccount */
	    assert($oAcc != null);

	    $oSrvSec = &App::getModuleService('Account', 'Security');
        $aMembData = $oSrvSec->getUserMembershipData();


        if (!$oSrvSec->isStaff($oAcc) && !$aMembData['design_tool'])
        {
            App::gotoUrl('public.denied');
        }

	    $oTemplate = $this->_oModule->getItem('Template');
	    /* @var  $oTemplate PhpFox_Mod_Account_ItemTemplate */
	    $oTemplate->loadByCondition('user = "'.$oAcc->aData['user'].'"');

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    // processing
        switch($oReq->getAction())
        {
            case 'reset_all':
                foreach(App::getParam('Mod_Account.design.parts') as $sParam)
                {
                    $oTemplate->aData[$sParam] = '';
                }
                $oTemplate->aData['bg_img'] = '';
                $oTemplate->aData['css'] = '';
				$aUpdFields = $oTemplate->aFields;
				unset($aUpdFields[$oTemplate->sId]);
				unset($aUpdFields['user']);
				$aUpdFields = array_keys($aUpdFields);
				$oTemplate->update($aUpdFields);
				$oUrl = &App::getUrl();
				/* @var $oUrl Url */
				$oUrl->setParam('act', '');
				App::forward($oUrl->getUrl());
                break;

            case 'reset_pic':
				$oTemplate->aData['bg_img'] = '';
				$oTemplate->update(array('bg_img'));
				$oUrl = &App::getUrl();
				/* @var $oUrl Url */
				$oUrl->setParam('act', '');
				App::forward($oUrl->getUrl());
                break;

            // ----- update design -----
            case 'update_design':

				$oTemplate->setData($oReq->getArray('aTempl'));
				$aUpdFields = $oTemplate->aFields;
				unset($aUpdFields[$oTemplate->sId]);
				unset($aUpdFields['user']);
				$aUpdFields = array_keys($aUpdFields);
				if ($oTemplate->update($aUpdFields))
			    {
			        $aMessages = array(App::format('Mod_Account.profile.design_updated'));
			        if (!empty($_FILES['image']['name']) && $this->_uploadImage($oAcc, 'image'))
			        {
			            $aMessages[] = App::format('Mod_Account.profile.design_image_uploaded');
			            $oTemplate->aData['bg_img'] = 'image';
			            $oTemplate->update(array('bg_img'));
			        }
			        $oUrl = &App::getUrl();
			        $oUrl->setParam('updated', 1);
			        App::forward($oUrl->getUrl(),$aMessages, 'message',
			                     App::getPageName(), 'ProfileDesign');
			    }
			    else
			    {
			        $this->addErrors($oAcc->getErrors());
			    }
				break;
        }


		$oTpl = &$this->_oModule->getTemplate();

		$oTpl->assignSrc(array(
		    'aUser'      => $oAcc->aData,
			'aParts'     => App::getParam('Mod_Account.design.parts'),
			'aTempl'     => $oTemplate->aData,
			'bUpdated'   => $oReq->get('updated', false),
			'sBgPicPath' => App::getParam('user_bg.pic.url'),
			'aErrors'    => $this->getErrors(),
			'sUserImage' => (App::getParam('force_re_cache') ? '?id=' . md5(rand(1003, 203948394)) : '')
		));
		$srcHtml = $oTpl->fetch('ProfileDesign.html');
		return $srcHtml;
	}

	/** Upload image
	 * @param PhpFox_Mod_Account $oAcc       user account
	 * @param string             $sFieldName form field name
	 * @return bool true - upload ok, false - error occured
	 */
	function _uploadImage(&$oAcc, $sFieldName = 'image')
	{
	    $oImage = &App::newObject('file_Image');
	    /* @var $oImage file_Image */
	    $sPicPath = App::getParam('user_bg.pic.path');
	    $bRes = $oImage->upload($sFieldName, $sPicPath, $oAcc->aData['user'], 'jpg');
	    if (! $bRes)
	    {
	        $this->addErrors($oImage->getErrors());
	    }
	    return $bRes;
	}
}
?>